#pragma once
#include <iostream>
#include <sys/select.h>
#include "Common.hpp"
#include "Socket.hpp"
#include <vector>
#include<unistd.h>
#include<sys/poll.h>
using namespace std;
using namespace SocketMoudle;
class pollServer
{
private:
    unique_ptr<Socket> _listenSocket;
    bool _running = true;
    const int defaulted = -1;
    static const int size = 4096;
    struct pollfd pfds[size];
public:
    pollServer(int port) : _listenSocket(make_unique<TcpSocket>(port))
    {
        _listenSocket->BuildListenSocket(port);
        // 初始化辅助数组
        for (int i = 0; i < size; i++)
        {
            pfds[i].fd = defaulted;
            pfds[i].events = 0;
            pfds[i].revents = 0;
        }
        pfds[0].fd = _listenSocket->GetSockFd();
        pfds[0].events = POLLIN;
    }
    ~pollServer() {}
    void start()
    {
        while (_running)
        {
            //int timeout=1000; // 超时时间(MS)
            int n = poll(pfds, size, -1);
            switch (n)
            {
            case -1:
                LOG(LogLevel::ERROR) << "select error";
                break;
            case 0:
                LOG(LogLevel::INFO) << "select timeout";
                break;
            default:
                LOG(LogLevel::INFO) << "select has event";
                Dispatcher(pfds);
                break;
            }
        }
        _running = false;
    }
    void Dispatcher(struct pollfd pfds[])
    {
        // 处理listenSocket的读事件
        for (int i = 0; i < size; i++)
        {
            if (pfds[i].revents & POLLIN)
            {
                if (pfds[i].fd == _listenSocket->GetSockFd())
                {
                    // 处理listenSocket的读事件
                    AcceptNewClient();
                }
                else
                {
                    // 处理其他客户端
                    Recever(i);
                }
            }
        }
    }
    void AcceptNewClient()
    {

        NetAddr client;
        int fd = _listenSocket->Accept(&client); // 不会阻塞，套接字已就绪
        if (fd >= 0)
        {
            LOG(LogLevel::INFO) << "accept a new client:" + client.stringaddr();
            // 将新的客户端加入到辅助数组中
            int i = 0;
            for (; i < size; i++)
            {
                if (pfds[i].fd == defaulted)
                {
                    break;
                }
            }
            if (i == size)
            {
                LOG(LogLevel::WARNING) << "达到最大连接数，无法接收新连接";
                 //如果是指针扩容即可，这里不做处理
                close(fd);
            }
            else
            {
                pfds[i].fd = fd;
                pfds[i].events = POLLIN;
                pfds[i].revents = 0;
            }
        }
    }
    void Recever(int i)
    {
        char buffer[1024];
        ssize_t n = recv(pfds[i].fd, buffer, sizeof(buffer) - 1, 0);
        if (n > 0)
        {
            buffer[n] = 0;
            cout<< "收到客户端数据:" << buffer;
        }
        else if (n == 0)
        {
            LOG(LogLevel::INFO) << "客户端断开连接";
            close(pfds[i].fd);
            // 将客户端fd从辅助数组中移除
            pfds[i].fd = defaulted;
            pfds[i].events = 0;
            pfds[i].revents = 0;
        }
        else
        {
            LOG(LogLevel::ERROR) << "接收数据失败";
            
        }
    }
    void stop()
    {
        _running = false;
    }
};